<template>
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="中间件" prop="netSysId"> 
            <el-select
                  v-model="queryParams.netSysId"
                  placeholder="中间件"
                  class="!w-240px" clearable
                >
                  <el-option
                    v-for="n in sysConfigList"
                    :key="n.id"
                    :label="n.community.name"
                    :value="n.id"
                  />
                </el-select>
          </el-form-item>
        <el-form-item label="分账模式" prop="divideMode">
            <el-select
              v-model="queryParams.divideMode"
              placeholder="分账模式"
              clearable
              class="!w-120px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.NET_DIVIDE_MODE)"
                :key="parseInt(dict.value)"
                :label="dict.label"
                :value="parseInt(dict.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="房产地址" prop="houseName">
            <el-input placeholder="请输入内容" v-model.trim="queryParams.houseName"/>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['zwsj:net-group-pay-house-map:create']"
            >
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['zwsj:net-group-pay-house-map:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="编号" align="center" prop="id" width="60"/>
          <el-table-column label="分账模式/地址" align="left" prop="houseName" min-width="280">
            <template #default="scope">
              <div>中间件：{{ scope.row.netSysName }}</div>
              <div>模式：<dict-tag :type="DICT_TYPE.NET_DIVIDE_MODE" :value="scope.row.divideMode" /></div>
              <div>地址：{{scope.row.houseName}}</div>
            </template>
          </el-table-column>
          <el-table-column label="兜底账户" align="left" prop="underTakeMatCode" width="200">
            <template #default="scope">
              <div>账户：{{scope.row.underTakeMatName_}}</div>
              <div>编码：{{ scope.row.underTakeMatCode }}</div>
            </template>
          </el-table-column>
          <el-table-column label="分账账户1/分账值1" align="left" prop="divideMatUserName1_" width="180">
            <template #default="scope">
              <div>账户1：{{scope.row.divideMatUserName1_}}</div>
              <div>值1：{{ scope.row.divideMatValue1 }}</div>
            </template>
          </el-table-column>
          <el-table-column label="分账账户2/分账值2" align="left" prop="divideMatUserName2_" width="180">
            <template #default="scope">
              <div>账户2：{{scope.row.divideMatUserName2_}}</div>
              <div>值2：{{ scope.row.divideMatValue2 }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180px">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="goAccounts( scope.row.id)"
                v-hasPermi="['zwsj:net-group-pay-house-map:update']"
              >
                账号列表
              </el-button>
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['zwsj:net-group-pay-house-map:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['zwsj:net-group-pay-house-map:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
  
  <!-- 表单弹窗：添加/修改 -->
  <NetGroupPayHouseMapForm ref="formRef" @success="getList" />
  <NetGroupPayAccountMap ref="netAccountMapRef"/>
  
  
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { NetGroupPayHouseMapApi, NetGroupPayHouseMapVO } from '@/api/zwsj/netgrouppayhousemap'
import { getNetSysConfigs } from '@/api/zwsj/netSysConfig'
import NetGroupPayHouseMapForm from './NetGroupPayHouseMapForm.vue'
import NetGroupPayAccountMap from './NetGroupPayAccountMap.vue'

/** 宽带批发配置（房产） 列表 */
defineOptions({ name: 'NetGroupPayHouseMap' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<NetGroupPayHouseMapVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  netSysId: undefined,
  houseId: undefined,
  houseName: undefined,
  divideMode: undefined,
  divideMatId1: undefined,
  divideMatValue1: undefined,
  divideMatId2: undefined,
  divideMatValue2: undefined,
  underTakeMatCode: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const sysConfigList = ref([])
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await NetGroupPayHouseMapApi.getNetGroupPayHouseMapPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
  sysConfigList.value = await getNetSysConfigs()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await NetGroupPayHouseMapApi.deleteNetGroupPayHouseMap(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await NetGroupPayHouseMapApi.exportNetGroupPayHouseMap(queryParams)
    download.excel(data, '宽带批发配置（房产）.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const netAccountMapRef = ref()
const goAccounts = (houseMapId: number) => {
  netAccountMapRef.value.open(houseMapId)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>